package cn.oms.oms.service.impl;

import cn.oms.oms.entity.Emr;
import cn.oms.oms.entity.Patient;
import cn.oms.oms.entity.Picking;
import cn.oms.oms.exception.ServiceException;
import cn.oms.oms.mapper.EmrMapper;
import cn.oms.oms.mapper.PatientMapper;
import cn.oms.oms.mapper.PickingMapper;
import cn.oms.oms.service.IPickingService;
import cn.oms.oms.vo.PickingVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@Service
public class PickingServiceImpl extends ServiceImpl<PickingMapper, Picking> implements IPickingService {

    @Autowired
    private EmrMapper emrMapper;

    @Autowired
    private PickingMapper pickingMapper;

    @Autowired
    private PatientMapper patientMapper;


    /**
     * 根据查询条件查询捡药列表
     * @param pickingVo
     * @return
     */
    @Override
    public PageInfo<Picking> selectPickingList(PickingVo pickingVo) {
        QueryWrapper<Picking> query = new QueryWrapper<>();
        QueryWrapper<Emr> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(pickingVo.getPatientName())){
            queryWrapper.like("patientName",pickingVo.getPatientName());
        }
        if (pickingVo.getPatientId()!=null&&!"".equals(pickingVo.getPatientId())){
            queryWrapper.eq("patientId",pickingVo.getPatientId());
        }
        queryWrapper.eq("deleteFlag",0);
        List<Emr> emrs = emrMapper.selectList(queryWrapper);
        List<Integer> patientIdList = new ArrayList<>();
        if (emrs.size()>0&&emrs!=null){
            for (Emr emr:emrs){
                patientIdList.add(emr.getPatientId());
            }
        }
        //patientIdList去重
        patientIdList = patientIdList.stream().distinct().collect(Collectors.toList());
        if (patientIdList.size()>0&&patientIdList!=null){
            query.in("patientId",patientIdList);
        }
        if (pickingVo.getPickingFlag()!=null&&!"".equals(pickingVo.getPickingFlag())){
            query.eq("pickingFlag",pickingVo.getPickingFlag());
        }
        if (pickingVo.getPayFlag()!=null&&!"".equals(pickingVo.getPayFlag())){
            query.eq("payFlag",pickingVo.getPayFlag());
        }
        if (pickingVo.getSendFlag()!=null&&!"".equals(pickingVo.getSendFlag())){
            query.eq("sendFlag",pickingVo.getSendFlag());
        }
        query.eq("deleteFlag",0);
        PageHelper.startPage(pickingVo.getPageNum(),pickingVo.getPageSize());
        List<Picking> pickingList = pickingMapper.selectList(query);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (pickingList.size()>0&&pickingList!=null){
            for (Picking picking:pickingList){
                QueryWrapper<Patient> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("patientId",picking.getPatientId());
                queryWrapper.eq("deleteFlag",0);
                Patient patient = patientMapper.selectOne(queryWrapper1);
                if (patient==null){
                    throw new ServiceException("参数异常，患者信息不存在");
                }
                picking.setReturnPatientName(patient.getPatientName());

                // 日期格式化
                picking.setReturnCreateTime(simpleDateFormat.format(picking.getCreateTime()));
            }
        }

        return new PageInfo<>(pickingList);
    }

    /**
     * 新增捡药
     * @param pickingVo
     * @return
     */
    @Override
    public String addPicking(PickingVo pickingVo) {
        return null;
    }

    /**
     * 修改捡药
     * @param pickingVo
     * @return
     */
    @Override
    public String updatePicking(PickingVo pickingVo) {

        if (pickingVo.getId()==null||"".equals(pickingVo.getId())){
            throw new ServiceException("参数异常，捡药ID不能为空");
        }
        if (pickingVo.getPickingQty()==null||"".equals(pickingVo.getPickingQty())){
            throw new ServiceException("参数异常，捡药量不能为空");
        }
        QueryWrapper<Picking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",pickingVo.getId());
        queryWrapper.eq("deleteFlag",0);
        Picking picking = pickingMapper.selectOne(queryWrapper);
        if (picking==null){
            throw new ServiceException("参数异常，未找到捡药发药信息");
        }
        if (picking.getSendFlag()==1){
            throw new ServiceException("参数异常，该条记录已发药");
        }
        if (picking.getPickingFlag()==1){
            throw new ServiceException("参数异常，该条记录已发药");
        }
        if (picking.getSendQty().compareTo(pickingVo.getPickingQty())!=0){
            throw new ServiceException("参数异常，捡药量不匹配，请重新捡药");
        }
        picking.setPickingQty(pickingVo.getPickingQty());
        picking.setPickingFlag(1);
        int num = pickingMapper.updateById(picking);
        if (num!=1){
            throw new ServiceException("数据库修改异常，修改捡药信息失败");
        }

        return "ok";
    }

    /**
     * 删除捡药
     * @param id
     * @return
     */
    @Override
    public String deletePicking(Integer id) {
        return null;
    }

    /**
     * 发药
     * @param pickingVo
     * @return
     */
    @Override
    public String sendPicking(PickingVo pickingVo) {
        if (pickingVo.getId()==null||"".equals(pickingVo.getId())){
            throw new ServiceException("参数异常，捡药ID不能为空");
        }
        if (pickingVo.getInputSenQty()==null||"".equals(pickingVo.getInputSenQty())){
            throw new ServiceException("参数异常，发药量不能为空");
        }
        QueryWrapper<Picking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",pickingVo.getId());
        queryWrapper.eq("deleteFlag",0);
        Picking picking = pickingMapper.selectOne(queryWrapper);
        if (picking==null){
            throw new ServiceException("参数异常，未找到捡药发药信息");
        }
        if (picking.getPayFlag()==0){
            throw new ServiceException("参数异常，该条记录未缴费，无法发药");
        }
        if (picking.getPickingQty().compareTo(pickingVo.getInputSenQty())!=0){
            throw new ServiceException("参数异常，发药量不匹配，请重新发药");
        }
        picking.setSendFlag(1);
        int num = pickingMapper.updateById(picking);
        if (num!=1){
            throw new ServiceException("数据库修改异常，修改捡药信息失败");
        }

        return "ok";
    }
}
